package ookweb;

import java.io.PrintStream;
import java.sql.ResultSet;
import java.sql.Statement;

public class Data {
	private Conn con = null;
	private Statement stmt;
	private ResultSet rs;

	public Data() {
	}

	public Data(String u, String p, String url) {
		this.con = new Conn(u, p, url);
	}

	public int getRowCount(String strSql) {
		int intCount = 0;
		try {
			this.stmt = this.con.getStmtread();
			this.rs = this.stmt.executeQuery("SELECT COUNT(*) FROM " + strSql);
			if (this.rs.next())
				intCount = this.rs.getInt(1);
			else
				intCount = -1;
		} catch (Exception e) {
			intCount = -2;
			System.err.println(e.getMessage());
			e.printStackTrace();
		} finally {
			this.con.close();
		}
		return intCount;
	}

	public int insert(String sql) {
		int count = 0;
		this.stmt = this.con.getStmt();
		try {
			count = this.stmt.executeUpdate(sql);
		} catch (Exception e) {
			count = -2;
			System.err.println(e.getMessage());
			e.printStackTrace();
		} finally {
			this.con.close();
		}
		return count;
	}

	public int update(String sql) {
		int count = 0;
		this.stmt = this.con.getStmt();
		try {
			count = this.stmt.executeUpdate(sql);
		} catch (Exception e) {
			count = -2;
			System.err.println(e.getMessage());
			e.printStackTrace();
		} finally {
			this.con.close();
		}
		return count;
	}

	public int delete(String sql) {
		int count = 0;
		this.stmt = this.con.getStmt();
		try {
			count = this.stmt.executeUpdate(sql);
		} catch (Exception e) {
			count = -2;
			System.err.println(e.getMessage());
			e.printStackTrace();
		} finally {
			this.con.close();
		}
		return count;
	}
}
